

import 'package:flutter/material.dart';
import 'package:toast/toast.dart';

import '../http/Api.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({super.key});

  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  String? _userName, _password, _repassword;
  final _formKey = GlobalKey<FormState>();
  final _scaffoldKey = GlobalKey<ScaffoldState>();

  FocusNode _userNameNode = new FocusNode();
  FocusNode _pwdNode = FocusNode();
  FocusNode _repasswordNode = FocusNode();

  bool _isObscure1 = true;
  bool _isObscure2 = true;
  Color _pwdIconColor1 = Colors.blue;
  Color _pwdIconColor2 = Colors.blue;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        leading: InkWell(
          onTap: (){
            Navigator.pop(context);
          },
          child: Icon(Icons.arrow_back,color: Colors.white),
        ),
        backgroundColor: Colors.blue,
        title: Text("注册",style: TextStyle(color: Colors.white)),
      ),
      //Form
      body: Form(
          key: _formKey,
          child: ListView(
            padding: EdgeInsets.fromLTRB(22.0, 18.0, 22.0, 0.0),
            children: <Widget>[
              _buildUserName(),
              _buildPwd(),
              _buildPwd2(),
              _buildRegister(),
            ],
          )),
    );
  }

  Widget _buildUserName() {
    return TextFormField(
      focusNode: _userNameNode,
      autofocus: true,
      decoration: InputDecoration(
        labelText: "用户名",
      ),
      textInputAction: TextInputAction.next,
      onEditingComplete: () {
        FocusScope.of(context).requestFocus(_pwdNode);
      },
      validator: (String? value) {
        if (value!.trim().isEmpty) {
          return "请输入用户名";
        }
        _userName = value;
        return null;
      },
    );
  }

  Widget _buildPwd() {
    return TextFormField(
      focusNode: _pwdNode,
      obscureText: _isObscure1,
      autofocus: true,
      decoration: InputDecoration(
        labelText: "密码",

          suffixIcon: IconButton(
            icon: Icon(Icons.remove_red_eye, color: _pwdIconColor1),
            onPressed: () {
              setState(() {
                _isObscure1 = !_isObscure1;
                _pwdIconColor1 = (_isObscure1
                    ? Colors.blue
                    : Theme.of(context).iconTheme.color)!;
              });
            },
          )
      ),
      textInputAction: TextInputAction.next,
      onEditingComplete: () {
        FocusScope.of(context).requestFocus(_repasswordNode);
      },
      validator: (String? value) {
        if (value!.trim().isEmpty) {
          //错误提示
          return "请输入密码";
        }
        _password = value;
        return null;
      },
    );
  }

  Widget _buildPwd2() {
    return TextFormField(
      focusNode: _repasswordNode,
      obscureText: _isObscure2,
      autofocus: true,
      decoration: InputDecoration(
        labelText: "确认密码",

          suffixIcon: IconButton(
            icon: Icon(Icons.remove_red_eye, color: _pwdIconColor2),
            onPressed: () {
              setState(() {
                _isObscure2 = !_isObscure2;
                _pwdIconColor2 = (_isObscure2
                    ? Colors.blue
                    : Theme.of(context).iconTheme.color)!;
              });
            },
          )
      ),
      textInputAction: TextInputAction.go,
      onEditingComplete: () {
        _click();
      },
      validator: (String? value) {
        if (value!.trim().isEmpty) {
          return "请确认密码";
        }

        if(_password != value){
          return "两次密码输入不一致";
        }
        _repassword = value;
        return null;
      },
    );
  }

  Widget _buildRegister() {
    return Container(
      height: 52.0,
      margin: EdgeInsets.only(top: 18.0),
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          backgroundColor: Theme.of(context).primaryColor,
        ),
        onPressed: _click,
        child: Text(
          "注册",
          style: TextStyle(color: Colors.white, fontSize: 18.0),
        ),
      ),
    );
  }

  _click() async{
    _userNameNode.unfocus();
    _pwdNode.unfocus();
    _repasswordNode.unfocus();

    if (_formKey.currentState!.validate()) {
      showDialog(context: context,barrierDismissible: false,builder: (_){
        return Center(child: CircularProgressIndicator(),);
      });

      var result =  await Api.register(_userName!,_password!);
      Navigator.pop(context);
      if(result['errorCode'] == -1){
        var error = result['errorMsg'];
        Toast.show(error,gravity: Toast.center);
      }else{
        Toast.show("注册成功!", gravity: Toast.center);
        Navigator.pop(context);
      }

    }
  }
}
